package com.jp.knowledge.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.adapter.team_circle.TeamCircleCompanySearchListAdapter;
import com.jp.knowledge.adapter.team_circle.TeamCircleCompanySelectedAdapter;
import com.jp.knowledge.dialog.TeamCircleCompanySelectDialog;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.activity.SearchListActivity;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.model.SearchResultModel;
import com.jp.knowledge.view.JpRecycleView;

import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class TeamCircleSearchCompanyActivity extends SearchListActivity implements TeamCircleCompanySearchListAdapter.OnSearchItemSelectedListener {

    public static final String SELECT_COMPANY_DATA = "selectCompanyData";

    @ViewInject(R.id.team_circle_company_selected_icon)
    private ImageView companySelectIcon;
    @ViewInject(R.id.team_circle_company_selected_rv)
    private JpRecycleView companySelectRv;
    @ViewInject(R.id.team_circle_company_selected_bn)
    protected TextView companySelectBn;

    protected TeamCircleCompanySearchListAdapter companySearchListadapter;
    private TeamCircleCompanySelectedAdapter selectedAdapter;
    private List<SearchResultModel> selectedModelList;

    private TeamCircleCompanySelectDialog companySelectDialog;

    @Override
    protected void init() {
        super.init();
        super.setCreateButton(3);

        requestCode = 0;
        topName.setText("其他公司");
        backgroundTV.setText("输入关键字搜索公司");

        companySearchListadapter = new TeamCircleCompanySearchListAdapter(this, new ArrayList<SearchResultModel>());
        companySearchListadapter.setOnSearchItemSelectedListener(this);
        searchList.setAdapter(companySearchListadapter);

        findViewById(R.id.team_circle_company_bottom_bar).setVisibility(View.VISIBLE);

        selectedModelList = (List<SearchResultModel>)
                getIntent().getSerializableExtra(SELECT_COMPANY_DATA);
        if(selectedModelList == null)
            selectedModelList = new ArrayList<>();
        companySelectRv.setHasFixedSize(true);
        selectedAdapter = new TeamCircleCompanySelectedAdapter(this, new ArrayList<SearchResultModel>());
        companySelectRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        companySelectRv.setAdapter(selectedAdapter);
        companySelectBn.setOnClickListener(this);
        selectedAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                TeamCircleSearchCompanyActivity.this.unSelect(position);
            }
        });

        companySelectDialog = new TeamCircleCompanySelectDialog(this);
        companySelectDialog.setIconClickListener(new TeamCircleCompanySelectDialog.IconClickListener() {
            @Override
            public void unSelect(int position) {
                TeamCircleSearchCompanyActivity.this.unSelect(position);
            }
        });
        setCompanySelectIcon();
        companySelectIcon.setOnClickListener(this);

        companySelectDialog.updateList(selectedModelList);
        selectedAdapter.updateAdapter(selectedModelList);
        companySelectBn.setText(String.format("确定(%d)", selectedModelList.size()));
    }

    public void unSelect(int position) {
        for(int i = 0; i < searchResults.size(); i++){
            if(searchResults.get(i).getId().equals(selectedModelList.get(position).getId())){
                onItemSelect(i, false);
                companySearchListadapter.updateAdapter(searchResults);
                companySelectDialog.updateList(selectedModelList);
                return;
            }
        }
        selectedModelList.remove(position);
        selectedAdapter.updateAdapter(selectedModelList);
        companySelectDialog.updateList(selectedModelList);
        companySelectBn.setText(String.format("确定(%d)", selectedModelList.size()));
        setCompanySelectIcon();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.team_circle_company_selected_bn){
            Intent intent = getIntent();
            intent.putExtra(SELECT_COMPANY_DATA, (Serializable)selectedModelList);
            setResult(RESULT_OK, intent);
            finishSelf();
        } else if(v.getId() == R.id.team_circle_company_selected_icon){
            companySelectDialog.show();
        } else {
            super.onClick(v);
        }
    }

    @Override
    public void onItemSelect(int position, boolean isChecked) {
        searchResults.get(position).setIsSelect(isChecked);
        if(isChecked){
            selectedModelList.add(searchResults.get(position));
        } else{
            for(SearchResultModel selectedModel : selectedModelList){
                if(selectedModel.getId().equals(searchResults.get(position).getId())){
                    selectedModelList.remove(selectedModel);
                    break;
                }
            }
        }
        selectedAdapter.updateAdapter(selectedModelList);
        companySelectDialog.updateList(selectedModelList);
        companySelectBn.setText(String.format("确定(%d)", selectedModelList.size()));
        setCompanySelectIcon();
    }

    @Override
    public void onNext(IModel data, int requestType) {
        super.onNext(data, requestType);
        for(SearchResultModel resultModel : searchResults){
            for(SearchResultModel selectedModel : selectedModelList){
                if(selectedModel.getId().equals(resultModel.getId())){
                    resultModel.setIsSelect(true);
                    break;
                }
            }
        }
        companySearchListadapter.updateAdapter(searchResults);
//        if(page <= 1) {
//            selectedModelList.clear();
//            selectedAdapter.updateAdapter(selectedModelList);
//        }
    }

    private void setCompanySelectIcon(){
        if(selectedModelList != null && selectedModelList.size() > 0) {
            companySelectIcon.setVisibility(View.VISIBLE);
        } else {
            companySelectIcon.setVisibility(View.GONE);
            companySelectDialog.dismiss();
        }
    }

    @Override
    protected void setCreateButton(int showType){}
}
